package com.bootdo.fx.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.bootdo.fx.domain.UserInfoDO;
import com.bootdo.fx.service.UserInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * 
 * @author 袁飞龙
 * @email 1992lcg@163.com
 * @date 2017-11-30 17:12:20
 */
 
@Controller
@SessionAttributes("userInfo")
public class UserInfoController {
	@Autowired
	private UserInfoService userInfoService;
	
	@GetMapping("/fx/userInfo")
	@RequiresPermissions("fx:userInfo:userInfo")
	String UserInfo(){
	    return "fx/userInfo/userInfo";
	}
	
	@ResponseBody
	@GetMapping("/fx/userInfo/list")
	@RequiresPermissions("fx:userInfo:userInfo")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<UserInfoDO> userInfoList = userInfoService.list(query);
		int total = userInfoService.count(query);
		PageUtils pageUtils = new PageUtils(userInfoList, total);
		return pageUtils;
	}
	
	@GetMapping("/fx/userInfo/add")
	@RequiresPermissions("fx:userInfo:add")
	String add(){
	    return "fx/userInfo/add";
	}

	@GetMapping("/fx/userInfo/edit/{userId}")
	@RequiresPermissions("fx:userInfo:edit")
	String edit(@PathVariable("userId") Long userId,Model model){
		UserInfoDO userInfo = userInfoService.get(userId);
		model.addAttribute("userInfo", userInfo);
	    return "fx/userInfo/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/fx/userInfo/save")
	@RequiresPermissions("fx:userInfo:add")
	public R save( UserInfoDO userInfo){
		if(userInfoService.save(userInfo)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/fx/userInfo/update")
	@RequiresPermissions("fx:userInfo:edit")
	public R update( UserInfoDO userInfo){
		userInfoService.update(userInfo);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/userInfo/remove")
	@ResponseBody
	@RequiresPermissions("fx:userInfo:remove")
	public R remove( Long userId){
		if(userInfoService.remove(userId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/userInfo/batchRemove")
	@ResponseBody
	@RequiresPermissions("fx:userInfo:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] userIds){
		userInfoService.batchRemove(userIds);
		return R.ok();
	}


	/*
	* 获取个人信息
	* */
	@RequestMapping("/wx/userInfo/getUserINfo")
	@ResponseBody
	public JsonResult getUserINfo(String userId,HttpServletRequest request, HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		//UserInfoDO userInfoDO = LoginUtils.getCurrentUser(request);
		UserInfoDO userInfoDO = userInfoService.get(Long.parseLong(userId));
		Long up = userInfoDO.getUpUserId();
		if(up == 0){
			userInfoDO.setUpUserName("系统平台");
		}else{
			UserInfoDO upUser = userInfoService.get(up);
			userInfoDO.setUpUserName(upUser.getUserName());
		}

		return ResultUtil.renderSuccess(userInfoDO);
	}

	/*
	* 修改个人信息
	* */
	@RequestMapping("/wx/userInfo/updateUserInfo")
	@ResponseBody
	public JsonResult updateUserInfo(UserInfoDO userInfo, HttpServletRequest request,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		//UserInfoDO ui = LoginUtils.getCurrentUser(request);
		//userInfo.setUserId(ui.getUserId());
		int num = userInfoService.update(userInfo);
		if(num > 0){
			return ResultUtil.renderSuccess("修改成功！");
		}else{
			return ResultUtil.renderError("修改失败！");
		}
	}
	
}
